home *** CD-ROM | disk | FTP | other *** search
- /************************************************************************/
- /* Project...: Standard ANSI-C Library */
- /* Name......: console.c */
- /* Purpose...: Stubs for console.c */
- /* Copyright.: ©Copyright 1994 by metrowerks inc. All rights reserved. */
- /************************************************************************/
-
- #ifndef __CONSOLE__
- #include <console.h>
- #endif
-
- /*
- * The following four functions provide the UI for the console package.
- * Users wishing to replace SIOUX with their own console package need
- * only provide the four functions below in a library.
- */
-
- /* I so wish... 04Jan95 e */
- #include <stdio.h>
- #include <signal.h>
- #include <errno.h>
- #include <unix.h>
- #include "os_mac.h"
- extern long gWaitTicksBG;
- extern long gWaitTicksFG;
-
- Boolean pause_atexit = 1;
- Boolean end_of_input = 0; // 19Jun96 e
- unsigned char *console_title = "\peConsole";
-
- /*
- * extern short InstallConsole(short fd);
- *
- * Installs the Console package, this function will be called right
- * before any read or write to one of the standard streams.
- *
- * short fd: The stream which we are reading/writing to/from.
- * returns short: 0 no error occurred, anything else error.
- */
-
- static WindowPeek eConsole;
-
- extern void (*__RemoveConsoleHandler__)(void);
-
- short InstallConsole(short fd)
- {
- #pragma unused( fd )
- if ( eConsole == NULL )
- {
- eConsole = os_console_new( console_title );
- _fcreator = CREATOR;
- _ftype = 'BINA'; /* will be set to TEXT for text files */
- #if ( __MWERKS__ < 0x1100 )
- __RemoveConsoleHandler__ = RemoveConsole;
- #endif
- }
- return 0;
- }
-
- /*
- * extern void RemoveConsole(void);
- *
- * Removes the console package. It is called after all other streams
- * are closed and exit functions (installed by either atexit or _atexit)
- * have been called. Since there is no way to recover from an error,
- * this function doesn't need to return any.
- */
-
- extern Boolean gDoQuit;
-
- void RemoveConsole(void)
- {
- #ifdef DoSCRIPT
- extern OSErr finish_AEDoScript( void );
- if( finish_AEDoScript() != noErr )
- {
- #endif
- #if pauseForQuitP
- /* pause for user acknowledgement */
- unsigned char buf[32];
- if ( pause_atexit && !gDoQuit )
- { SetWTitle( (WindowPtr )eConsole, "\ppress «enter» to exit" );
- while ( os_console_read_nohang( eConsole, buf, 32 ) == 0 ) /* wait */ ;
- }
- #endif
- #ifdef DoSCRIPT
- }
- #endif
- }
-
- /*
- * extern long WriteCharsToConsole(char *buffer, long n);
- *
- * Writes a stream of output to the Console window. This function is
- * called by write.
- *
- * char *buffer: Pointer to the buffer to be written.
- * long n: The length of the buffer to be written.
- * returns short: Actual number of characters written to the stream,
- * -1 if an error occurred.
- */
-
- long WriteCharsToConsole(char *buffer, long n)
- {
- return os_console_write( eConsole, (unsigned char *)buffer, n );
- }
-
- /*
- * extern long ReadCharsFromConsole(char *buffer, long n);
- *
- * Reads from the Console into a buffer. This function is called by
- * read.
- *
- * char *buffer: Pointer to the buffer which will recieve the input.
- * long n: The maximum amount of characters to be read (size of
- * buffer).
- * returns short: Actual number of characters read from the stream,
- * -1 if an error occurred.
- */
-
- long ReadCharsFromConsole(char *buffer, long n)
- { long result = 0;
- #if ReadHangsP
- int idle = 0;
- #if EndOfInputP == 2
- if (end_of_input)
- { end_of_input = 0;
- *buffer = 0;
- return 0;
- }
- #endif
- while ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
- { if (interrupted)
- { interrupted = 0;
- raise( SIGINT );
- errno = EINTR;
- break;
- }
- #if EndOfInputP == 2
- if (end_of_input)
- { end_of_input = 0;
- *buffer = 0;
- break;
- }
- #endif
- #if EndOfInputP == 1
- if (end_of_input) break;
- #endif
- if ( idle < 8 )
- { idle++;
- gWaitTicksBG <<= 1;
- gWaitTicksFG <<= 1;
- }
- }
- #if EndOfInputP == 1
- end_of_input = 0;
- #endif
- gWaitTicksBG >>= idle;
- gWaitTicksFG >>= idle;
- #else
- if ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
- { result = 0; /* EOF ? */
- }
- #endif
- return result;
- }
-
- /* e */
-
- long CharReadyConsoleP(void)
- {
- return os_console_chars_ready( eConsole );
- }
-
- /* end */
-